package com.dfhc.tag;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.DynamicAttributes;

import org.apache.commons.collections.MapUtils;
import org.quickbundle.project.RmGlobalReference;
import org.quickbundle.project.RmProjectHelper;
import org.quickbundle.project.common.vo.RmCommonVo;

import com.dfhc.ISystemConstant;
import com.dfhc.util.StringHelper;
import com.dfhc.util.TemplateHelper;

import freemarker.template.TemplateException;
 
public class FrameworkSelectTag extends BodyTagSupport implements DynamicAttributes {
    private Map attributesMap = new HashMap();
 
    @Override
    public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
        this.attributesMap.put(name, value);
    }
   
    private String buildSelectHTML() throws IOException, TemplateException{
        //option
        Map<String,Object> optionMap=getSqlOption(attributesMap);
        
        String defaultValue = MapUtils.getString(attributesMap, ISystemConstant.DICTIONARY_FRAMEWORK_DEFAULT_DEFAULTVALUE);
        String templateKey ;
		if(!attributesMap.containsKey(ISystemConstant.TAG_TEMPLATE_ID)){
			templateKey= ISystemConstant.DICTIONARY_TAG_SELECT_TEMPLATE;
		}else{
			templateKey = (String)attributesMap.get(ISystemConstant.TAG_TEMPLATE_ID);
		}
        String selectHtml =RmGlobalReference.get(ISystemConstant.DICTIONARY_TAG_TEMPLATE, templateKey);
        if(StringHelper.isEmpty(selectHtml)){
        	throw new IOException("下拉框尚未配置模板(模板Id:"+templateKey+")，请在编码表增加!");
        }
        Map<String,Object> rootMap = new HashMap<String,Object>();
		//属性map
        rootMap.put(ISystemConstant.ATTRIBUTES_MAP_KEY, attributesMap);
        //下拉项map
		rootMap.put(ISystemConstant.OPTIONS_MAP_KEY,optionMap);
		//默认选中项
		if(!StringHelper.isEmpty(defaultValue)){
			rootMap.put(ISystemConstant.SELECT_DEFAULT_VALUE, defaultValue);
		}
		//是否出现”--请选择--"
		String pleaseSelect = MapUtils.getString(attributesMap, ISystemConstant.PLEASE_SELECT);
		if(!StringHelper.isEmpty(pleaseSelect)){
			rootMap.put(ISystemConstant.PLEASE_SELECT, pleaseSelect);
		}
        return TemplateHelper.createContentByString(rootMap, selectHtml);
        
    }
   
    public  Map<String, Object>  getSqlOption(Map attributesMap) throws IOException, TemplateException{    	
        //sqlOption
        String type = MapUtils.getString(attributesMap, ISystemConstant.DICTIONARY_FRAMEWORK_DEFAULT_SQLOPTION);// 区分编码表  还是sql
        if(StringHelper.isEmpty(type)){
        	throw new IOException("缺少sqlOption属性!");
        }        
        switch (type.toUpperCase()) {
	        case "SQL":
//	        	columnId
	        	String columnId  = MapUtils.getString(attributesMap, ISystemConstant.DICTIONARY_FRAMEWORK_DEFAULT_COLUMNID);
	        	//columnName
	        	String columnName  = MapUtils.getString(attributesMap, ISystemConstant.DICTIONARY_FRAMEWORK_DEFAULT_COLUMNNAME);
	        	//sql
	        	String sql  = MapUtils.getString(attributesMap, ISystemConstant.DICTIONARY_FRAMEWORK_SELECT_SQL);
//	        	conditions
//	        	String conditions = MapUtils.getString(attributesMap, ISystemConstant.DICTIONARY_FRAMEWORK_DEFAULT_CONDITIONS);
	        	
	        	//String sql="select "+columnId+"  as id, "+columnName+" as  name     from "+tableName+" "+conditions;
	        	if(StringHelper.isEmpty(columnId)){
	        		throw new IOException("缺少columnId属性!");
	        	}
	        	if(StringHelper.isEmpty(columnName)){
	        		throw new IOException("缺少columnName属性!");
	        	}
	        	if(StringHelper.isEmpty(sql)){
	        		throw new IOException("缺少sql属性!");
	        	}
	        	
				List<RmCommonVo> lvos = RmProjectHelper.getCommonServiceInstance().doQuery(sql);
				if(!lvos.isEmpty()){
					Map<String, Object>  map=null;
					map=new HashMap<String, Object>();
					for (int i = 0; i < lvos.size(); i++) {
						map.put(lvos.get(i).getString(columnId),lvos.get(i).get(columnName));						
					}
					return map;
				}
	        	
	        	break;
	        case "CODE":
	        	Map<String, Object>   map =RmGlobalReference.get(MapUtils.getString(attributesMap, ISystemConstant.DICTIONARY_FRAMEWORK_DEFAULT_CODE));
	        	return map;
	        default:
	            break;
        }
        
        
    	return  new HashMap<String,Object>();
    }
    
    
    
    @Override
    public int doStartTag() throws JspTagException {
        return EVAL_BODY_BUFFERED;
    }
   
    @Override
    public int doEndTag() throws JspTagException {
        String result = "";
        try {
                result = this.buildSelectHTML();
            super.pageContext.getOut().print(result);
        } catch (Exception e) {
            throw new JspTagException(e.getMessage());
        }
        this.attributesMap.clear();
        return SKIP_BODY;
    }

}
